package cn.kow.kmall.controller.system;

import cn.kow.kmall.controller.BaseController;
import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.system.entity.SysDict;
import cn.kow.kmall.system.entity.SysDictData;
import cn.kow.kmall.admin.pojo.dto.DictDataQueryDTO;
import cn.kow.kmall.admin.pojo.dto.DictQueryDTO;
import cn.kow.kmall.system.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-10-29 17:04
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("/dict")
public class SysDictController extends BaseController {

    @Autowired
    private SysDictService dictService;

    /**
     * 新增字典类型
     */
    @PostMapping(value = "/add")
    public KmallResult add(@RequestBody SysDict sysDict) {
        sysDict.setCreateBy(currentUser().getUsername());
        dictService.save(sysDict);
        return KmallResult.success();
    }

    @DeleteMapping("/{dictIds}")
    public KmallResult remove(@PathVariable Long[] dictIds) {
        List<Long> idList = Arrays.asList(dictIds);
        dictService.remove(idList);
        return KmallResult.success();
    }

    @PostMapping(value = "/edit")
    public KmallResult edit(@Validated @RequestBody SysDict sysDict) {
        sysDict.setUpdateBy(currentUser().getUsername());
        dictService.update(sysDict);
        return KmallResult.success();
    }

    /**
     * 字典列表
     * @param queryDTO
     * @return
     */
    @PostMapping("/list")
    public KmallResult list(@RequestBody DictQueryDTO queryDTO) {
        Page<SysDict> dictPage = dictService.listDictByPage(queryDTO);
        List<SysDict> dictList = dictPage.getContent();
        return KmallResult.success()
                .put("rows", dictList)
                .put("total", dictPage.getTotalElements());
    }

    @GetMapping("/option/list")
    public KmallResult listAll() {
        List<SysDict> dictList = dictService.listDict();
        return KmallResult.success(dictList);
    }

    /**
     * 查询字典类型详细
     */
    @GetMapping(value = "/{dictId}")
    public KmallResult getInfo(@PathVariable Long dictId) {
        SysDict dict = dictService.getById(dictId);
        return KmallResult.success(dict);
    }

    // @PageableDefault(sort = {"sort"}, direction = Sort.Direction.ASC) Pageable pageable
    @PostMapping("/data/list")
    public KmallResult dataList(@RequestBody DictDataQueryDTO queryDTO) {
        Page<SysDictData> dataPage = dictService.listDictDataByPage(queryDTO);
        return KmallResult.success()
                .put("rows", dataPage.getContent())
                .put("total", dataPage.getTotalElements());
    }

    /**
     * 查询字典数据详细
     */
    @GetMapping(value = "/data/{id}")
    public KmallResult getDataInfo(@PathVariable Long id) {
        SysDictData dictData = dictService.getDictDataById(id);
        return KmallResult.success(dictData);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/data/type/{dictType}")
    public KmallResult dictType(@PathVariable String dictType) {
        List<SysDictData> list = dictService.listDictDataByType(dictType);
        return KmallResult.success(list);
    }

    /**
     * 新增字典类型
     */
    @PostMapping(value = "/data/add")
    public KmallResult addData(@Validated @RequestBody SysDictData dictData) {
        dictData.setCreateBy(currentUser().getUsername());
        dictData.setCreateTime(new Date());
        dictData.setUpdateBy(currentUser().getUsername());
        dictData.setUpdateTime(new Date());
        dictService.addDictData(dictData);
        return KmallResult.success();
    }

    /**
     * 修改保存字典类型
     */
    @PostMapping(value = "/data/edit")
    public KmallResult editData(@Validated @RequestBody SysDictData dictData) {
        dictData.setUpdateBy(currentUser().getUsername());
        dictService.updateDictData(dictData);
        return KmallResult.success();
    }

    /**
     * 删除字典类型
     */
    @PostMapping("/data/del")
    public KmallResult removeData(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        dictService.deleteDictDataByIds(idList);
        return KmallResult.success();
    }
}

